package com.spa.infrastructure.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.command.admin.ResourceAddCommand;
import com.spa.application.command.admin.ResourceEditCommand;
import com.spa.application.dto.admin.ResourceDTO;
import com.spa.application.dto.admin.menu.RouterDTO;
import com.spa.application.dto.admin.menu.geeker.MenuDTO;
import com.spa.domain.entity.Resource;
import com.spa.infrastructure.repository.po.ResourcePO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 对象映射框架配置.
 *
 * @author liangjun
 */
@Mapper
public interface ResourceMapStruct {

    ResourceMapStruct INSTANCE = Mappers.getMapper(ResourceMapStruct.class);

    /**
     * ResourcePO 转换为 Resource.
     */
    Resource resourcePOToResource(ResourcePO resourcePO);

    /**
     * ResourceAddCommand 转换为 ResourcePO.
     */
    ResourcePO addCommandToResourcePO(ResourceAddCommand command);

    /**
     * ResourceEditCommand 转换为 ResourcePO.
     */
    ResourcePO editCommandToResourcePO(ResourceEditCommand command);

    /**
     * Resource 转换为 ResourceDTO.
     */
    List<ResourceDTO> toResourceDTOList(List<Resource> resourceList);

    @Mapping(source = "resourceName", target = "name")
    @Mapping(source = "resourceUrl", target = "path")
    ResourceDTO toResourceDTO(Resource resource);

    /**
     * ResourcePO 转换为 Resource.
     */
    List<Resource> toResourceList(List<ResourcePO> resourceList);

    /**
     * ResourcePO 转换为 Resource.
     */
    Page<Resource> toResourcePage(Page<ResourcePO> resourceList);


    /**
     * ResourceDTO 转换为 RouterDTO.
     */
    List<RouterDTO> resourceDTOToRouterDTOList(List<ResourceDTO> resourceDTOList);

    @Mapping(source = "name", target = "meta.title")
    @Mapping(source = "icon", target = "meta.icon")
    @Mapping(source = "resourceCode", target = "name")
    RouterDTO resourceDTOToRouterDTO(ResourceDTO resourceDTO);

    List<MenuDTO> resource2Menu4List(List<ResourceDTO> resourceDTOList);

    @Mapping(source = "parentId", target = "pid")
    @Mapping(source = "name", target = "meta.title")
    @Mapping(source = "resourceCode", target = "component")
    MenuDTO resource2Menu(ResourceDTO resourceDTO);
}
